﻿CREATE FUNCTION QIC.InterfaceFactory_IF (@ConfigName VARCHAR(50), @InterfaceId INT)
RETURNS NVARCHAR(MAX)
AS
BEGIN

	DECLARE @Retval VARCHAR(MAX)
	DECLARE @Parameters VARCHAR(MAX)
	DECLARE @ObjectName VARCHAR(MAX)
	DECLARE @HasDelegateName BIT
	DECLARE @Implementation VARCHAR(MAX)
	DECLARE @NonDefaultDelegates VARCHAR(MAX)
	DECLARE @ReturnToken VARCHAR(MAX)

	DECLARE @Delegate TABLE (DelegateId INT, InterfaceId INT, DelegateName VARCHAR(50), InterfaceType VARCHAR(50), TokenMap VARCHAR(50), CallFactory VARCHAR(50))
	DECLARE @Token QIC.TokenTable

	INSERT INTO @Delegate (InterfaceId,DelegateId,DelegateName,InterfaceType,TokenMap, CallFactory)
	SELECT InterfaceId,DelegateId, DelegateName, InterfaceType, TokenMap, CallFactory
	FROM QIC.Delegate_(@ConfigName) delegate
	WHERE InterfaceId = @InterfaceId

	INSERT INTO @Token (ObjectId, PropertyId, PropertyName, UserTypeId, Precision, Scale, MaxLength, IsOutPut, IsReadOnly)
	SELECT parameters.object_id, parameters.parameter_id, case parameters.parameter_id WHEN 0 THEN @ReturnToken ELSE parameters.name END, parameters.user_type_id, parameters.precision, parameters.scale, parameters.max_length, parameters.is_output, parameters.is_readonly
	FROM sys.parameters parameters
	WHERE parameters.object_id = @InterfaceId
	AND parameters.parameter_id <> 0
	ORDER BY parameters.parameter_id

	IF EXISTS (select * from @Token where PropertyName = '@DelegateName') 
	BEGIN
		SET @HasDelegateName = 1
	END
	ELSE
	BEGIN
		SET @HasDelegateName = 0
	END

	SELECT @ObjectName = QIC.ObjectName(@InterfaceId)

	SET @Parameters = ''
	SET @Implementation = ''
	SET @NonDefaultDelegates = ''

	SELECT @NonDefaultDelegates += '''' + delegate.DelegateName + ''','	FROM @Delegate delegate

	IF LEN(@NonDefaultDelegates) > 0 SET @NonDefaultDelegates = LEFT(@NonDefaultDelegates,LEN(@NonDefaultDelegates)-1)

	SELECT @Parameters += QIC.TokenDefine(parameters.name, user_type_id, precision, scale, max_length, is_output, is_readonly) + ', '
	FROM sys.parameters parameters
	WHERE parameters.object_id = @InterfaceId
	AND parameters.name <> ''
	ORDER BY parameter_id

	IF @Parameters <> '' SET @Parameters = LEFT(@Parameters,DATALENGTH(@Parameters) - 2)

	SELECT @Implementation += 
		QIC.CallFactory(@ConfigName,@Token, @InterfaceId, @InterfaceId,'','', 1, '', 'IF') + ' WHERE 1=0' +
		QIC.LB() + QIC.Indent(1) + 'UNION ALL' + QIC.LB() 

	SELECT @Implementation += 
		QIC.CallFactory(@ConfigName,@Token,delegate.InterfaceId, delegate.DelegateId,'',delegate.TokenMap, 1, delegate.DelegateName, delegate.CallFactory) +
		CASE @HasDelegateName 
			WHEN 1 THEN ' WHERE @DelegateName=' + QUOTENAME(delegate.DelegateName,'''') +  + ' ' 
			ELSE '' 
		END +
		QIC.LB() + QIC.Indent(1) + 'UNION ALL' + QIC.LB() 
	FROM @Delegate delegate

	IF @HasDelegateName = 1 AND EXISTS(SELECT * FROM @Delegate WHERE DelegateName = 'DEFAULT')
	BEGIN
		SELECT @Implementation += 
			QIC.CallFactory(@ConfigName,@Token,delegate.InterfaceId, delegate.DelegateId,'',delegate.TokenMap, 1, delegate.DelegateName, delegate.CallFactory) +
			CASE @HasDelegateName 
				WHEN 1 THEN ' WHERE @DelegateName NOT IN (' + @NonDefaultDelegates + ') ' 
				ELSE '' 
			END +
			QIC.LB() + QIC.Indent(1) + 'UNION ALL' + QIC.LB() 
		FROM @Delegate delegate
		WHERE DelegateName = 'DEFAULT'
	END

	IF @Implementation <> '' SET @Implementation = LEFT(@Implementation,DATALENGTH(@Implementation) - 11)

	IF @Implementation = ''
	BEGIN
		SET @Implementation += QIC.Indent(1) + QIC.CallFactory(@ConfigName,@Token,@InterfaceId, null,'',null, 1, '','IF') + ' WHERE 1 = 0'  + QIC.LB()
	END

	SET @Retval = 'ALTER FUNCTION ' + @ObjectName + '(' + @Parameters + ')' + QIC.LB()
	SET @Retval += 'RETURNS TABLE ' + QIC.LB()
	SET @Retval += 'AS' + QIC.LB()
	SET @Retval += 'RETURN' + QIC.LB()
	SET @Retval += '(' + QIC.LB()
	SET @Retval += ISNULL(@Implementation + QIC.LB(),'')		
	SET @Retval += ');' 

	RETURN @Retval

END